﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 系统表仓储层
    /// </summary>
    public class SystemInfoRepository : SqlSugarRepository<SystemInfoEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public SystemInfoRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<int> GetMaxId()
        {
            return await _unitOfWork.QueryClient.Queryable<SystemInfoEntity>().MaxAsync(it => it.Id);
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<SystemInfoListDto>> GetPageList(SystemInfoQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<SystemInfoEntity>()
                .Where(p => p.IsDeleted == 0 && (input.ASType == null || input.ASType<=0 || (input.ASType>0 && p.SystemType == input.ASType)) && p.Name.Contains(input.Keywords));
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .Select(it => new SystemInfoListDto()
                {
                    Id = it.Id,
                    SystemKey = it.SystemKey,
                    SystemType = it.SystemType,
                    Name = it.Name,
                    CnName = it.CnName,
                    EnName = it.EnName,
                    BucketName = it.BucketName,
                    Icon = it.Icon,
                    LogoUrl = it.LogoUrl,
                    FrontPort = it.FrontPort,
                    DevUrl = it.DevUrl,
                    TestUrl = it.TestUrl,
                    PreUrl = it.PreUrl,
                    ProUrl = it.ProUrl,
                    CodeUrl = it.CodeUrl,
                    HeaderIds = it.HeaderIds,
                    HeaderNames = it.HeaderNames,
                    Sort = it.Sort,
                    Remark = it.Remark
                })
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<SystemInfoListDto>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 系统英文名称是否存在
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ExistEnName(int id, string enName)
        {
            return await _unitOfWork.QueryClient.Queryable<SystemInfoEntity>().Where(it => it.EnName == enName && it.IsDeleted == 0 && it.Id != id).CountAsync() > 0;
        }

        /// <summary>
        /// 系统Key是否存在
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ExistSystemKey(int id, string systemKey)
        {
            return await _unitOfWork.QueryClient.Queryable<SystemInfoEntity>().Where(it => it.SystemKey == systemKey && it.IsDeleted == 0 && it.Id != id).CountAsync() > 0;
        }
        
    }
}
